#pragma once
#include"common.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
// c++ 文件操作·
#include <fstream>
#include"Log.hpp"

const std::string gpath = "./";
const std::string gdictname = "dict.txt";
const std::string gstep = ": ";
using namespace LogMudule;
class dict
{
public:
    dict(std::string &path, std::string &filename)
        : _path(path), _filename(filename)
    {
        loadDict();
        print();
        // std::cout << "print execute" << std::endl;
    }
    std::string translate(const std::string &world) const
    {
        auto iter = _dict.find(world);
        if (iter == _dict.end()) return "None";

        return iter->second;
    }
    void print()
    {
        for (auto& w : _dict)
        {
            std::cout << w.first << " : " << w.second << std::endl;
        }
    }
    dict()
    {
        loadDict();
        print();
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _path = gpath;
    std::string _filename = gdictname;

    bool loadDict()
    {
        std::string file = _path + _filename;
        std::ifstream in(file.c_str());
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file" << file << "error"; 
            return false;
        }
        std::string line;
        while (std::getline(in, line))
        {
            std::string key;
            std::string value;
            if(splitString(line, &key, &value, gstep))
            {
                _dict.emplace(key, value);
            }
        }
        
        in.close();

        return true;
    }
};
